package com.example.management.config

import com.example.management.blog.service.impl.ContentService
import com.example.management.common.utils.PageUtils
import com.example.management.common.utils.Query
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.reactive.function.server.HandlerFunction
import org.springframework.web.reactive.function.server.ServerResponse
import org.springframework.web.reactive.function.server.RequestPredicates
import org.springframework.web.reactive.function.server.RouterFunctions
import org.springframework.web.reactive.function.server.RouterFunction
import reactor.core.publisher.Mono


/**
 *  路由器函数配置
 */
@Configuration
class RouterFunctionConfiguration {


    @Bean
    fun opentList(bContentService: ContentService): RouterFunction<ServerResponse> {
        return RouterFunctions.route( RequestPredicates.GET("/blog/open/list2"),
                HandlerFunction {

                    var type = it.queryParam("type");
                    var limit = it.queryParam("limit");
                    var offset = it.queryParam("offset");
                    var map = HashMap<String,String>();
                    map.put("type", type.get())
                    map.put("limit",limit.get())
                    map.put("offset",offset.get())
                    val query = Query(map)
                    val bContentList = bContentService?.list(query)
                    val total = bContentService?.count(query)
                    var pageUtils = PageUtils(bContentList, total!!)
                    var respone: Mono<PageUtils> = Mono.justOrEmpty(pageUtils);
                    ServerResponse.ok().body(respone, PageUtils::class.java);
        });

    }


}